﻿function stateChanged() {
    if (xmlhttp.readyState == 4) {
        if (receiveid == "filtercontent") {
            try {
                if (xmlhttp.responseText != '') {
                    var arr = xmlhttp.responseText.split("~")
                    var fill = eval('(' + arr[0] + ')');
                    // document.getElementById(receiveid).innerHTML = 
                    fill_cashbook(fill,arr[1],arr[2]);
                }
                else {
                    document.getElementById(receiveid).innerHTML = "<center class='green'><span>No. record found.</span></center>";
                }

            } catch (e) {
                document.getElementById(receiveid).innerHTML = "<center class='red'><span>Error : Server seems to be very busy.Please try after some time. </span></center>";
            }
        }
        else {
            document.getElementById(receiveid).innerHTML = xmlhttp.responseText;
        }
    } else {
            document.getElementById(receiveid).innerHTML = "<div align='center'><img src='images/loading.gif' /></div>";
    }
    }
   
    function printreceipt() {
        var currentTime = new Date()
        var month = currentTime.getMonth() + 1
        var day = currentTime.getDate()
        var year = currentTime.getFullYear()

        var wn = window.open('Accession Register', '', 'width=900,height=900,scrollbars=1');
        var str = "";
        str += "<html><head><title>Print Preview::Day Book</title></head><body><div> <b> Cash Book </b> </center> </div> <div> Today Date = " + month + "/" + day + "/" + year + " </div><div>" + document.getElementById('print').innerHTML + "</div></body></html>"
        wn.document.write(str);
        wn.print();

    }
    function remRow() {
        var ele = document.getElementById("cashbook_tab");
        for (var i = parseInt(ele.rows.length)-2; i > 1; i--) {
            ele.deleteRow(i);
        }
    }
    function collectdata() {
        var frm = document.forms[0];
        var st = "";
        var sel = document.getElementsByTagName("select");
        for (var i = 0; i < sel.length; i++) {
            st += "&" + sel[i].name + "=" + sel[i].value;
        }
        var sel = document.getElementsByTagName("textarea");
        for (var i = 0; i < sel.length; i++) {
            st += "&" + sel[i].name + "=" + sel[i].value;
        }
        for (var i = 0; i < frm.length; i++) {
            if (frm[i].type == 'text' || frm[i].type == 'password')
                st += "&" + frm[i].name + "=" + frm[i].value;
        }
        return st;
    }
    function FindGroup() {
        if (validate()) {
            if (document.getElementById('filtercontent')) {
                document.getElementById('filtercontent').innerHTML = ''
            };
            senddata_post('Rpt_ADayBook', 'T=PrimaryGroup' + collectdata(), 'PrimaryGroup');
        } 
    }

    function gofilter(val) {
        if (validate()) {
            var str = "";
            var x = document.getElementById("PrimaryGrp").selectedIndex;
            var y = document.getElementById("PrimaryGrp").options;
            var datefrom = "";
            if (val == "paging") {
                datefrom = document.getElementById('pagingDate').value;
                if (document.getElementById('filtercontent')) {
                    document.getElementById('filtercontent').setAttribute("onclick","") 
                }
            }
            else {
                datefrom = document.getElementById('txtdatefrom').value;
                if (document.getElementById('content')) {
                    document.getElementById('content').style.display = 'block';
                }
                if (document.getElementById('filtercontent')) {
                    document.getElementById('filtercontent').innerHTML = "";
                    document.getElementById('filtercontent').style.visibility = 'visible';
                    remRow();
                }
                if (document.getElementById('paging')) {
                    document.getElementById('paging').style.visibility = 'visible';
                }
                if (document.getElementById('excel')) {
                    document.getElementById('excel').style.display = "none";
                }
            }
            if (document.getElementById("sPGroupName")) {
                document.getElementById("sPGroupName").innerHTML = y[x].innerHTML;
            }
            senddata_post('Rpt_ADayBook', 'T=Filter' + collectdata()+'&GrpNme=' + y[x].innerHTML + '&datefrom=' + datefrom, 'filtercontent');
        }
    }
    function fill_cashbook(data,date,last) {
        var ele = document.getElementById("cashbook_tab");
        for (var i = 0; i < data.length; i++) {
            var index = parseInt(ele.rows.length) - 1;
            var x = ele.insertRow(index);
            var cl = new Array();
            cl[0] = x.insertCell(0);
            cl[1] = x.insertCell(1);
            cl[2] = x.insertCell(2);
            cl[3] = x.insertCell(3);
            cl[4] = x.insertCell(4);
            cl[5] = x.insertCell(5);
            cl[0].innerHTML = data[i].RECEIPT;
            cl[0].style = "border-left:1px solid #000";
            cl[1].innerHTML = data[i].CVOUCHER;
            cl[2].innerHTML = data[i].CAMOUNT;
            cl[2].style = "border-right:1px solid #000;text-align:right";
            cl[3].innerHTML = data[i].PAYMENT;
            cl[3].style = "border-left:1px solid #000";
            cl[4].innerHTML = data[i].DVOUCHER;
            cl[5].innerHTML = data[i].DAMOUNT;
            cl[5].style = "border-right:1px solid #000;text-align:right";

        }
        if (document.getElementById('filtercontent')) {
            if (parseInt(last) == 1) {
                document.getElementById('filtercontent').innerHTML = "";
                document.getElementById('filtercontent').setAttribute("onclick", "");
            } else {
                document.getElementById('filtercontent').innerHTML = "Click Here For More Result";
                document.getElementById('filtercontent').style.visibility = 'visible';
                document.getElementById('pagingDate').value = date;
                document.getElementById('server_last').value = last;
                document.getElementById('filtercontent').setAttribute("onclick", "gofilter('paging')")
             }
        }
        if (document.getElementById('excel')) {
            document.getElementById('excel').style.display = "block";
        }
    }
